/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.core.cluster;

import org.apache.mina.common.IoSession;

import com.cqcis.uip.base.framework.UIPServiceException;
import com.cqcis.uip.base.framework.common.HttpParam;
import com.cqcis.uip.base.framework.common.Task;
import com.cqcis.uip.base.framework.common.HttpParam.ParamContent;
import com.cqcis.uip.base.framework.common.Task.Type;
import com.cqcis.uip.base.framework.core.Endpoint;
import com.cqcis.uip.base.framework.utils.ByteBuffer;
import com.cqcis.uip.base.framework.utils.EndpointGenerator;
import com.cqcis.uip.base.framework.utils.StringUtil;

/**
 * 集群支持, 只支持task提交方式
 * 
 * @author huangmy
 * @date 2008-12-31
 */
public final class ClusterSwitchSupport {
	public final static String PARAM_ENDPOINT_FROM = "uip-endpoint-from"; // from
	public final static String PARAM_ENDPOINT_TO = "uip-endpoint-to"; // to	
	public final static String PARAM_TASK_TYPE = "uip-task-type"; // the type of task
		
	private ClusterSwitchSupport() {		
	}
	
	/**
	 * 类型
	 * 
	 * @param p
	 * @return
	 */
	public static Type getType(HttpParam p) {
		String type = null;
		ParamContent taskType = p.getParam(PARAM_TASK_TYPE);
		if (taskType == null
				|| (type = StringUtil.trimWhiteToNull(taskType.getValue())) == null) {
			return null;
		}
		
		return Type.parse(type);
	}
	
	
	
	/**
	 * 获取需要提交的目的RS
	 * 
	 * @param p
	 * @return
	 * @throws Exception 
	 */
	public static Endpoint getToEndpoint(HttpParam p) throws Exception {
		ParamContent endpointToContent = p.getParam(PARAM_ENDPOINT_TO);
		if (endpointToContent == null
				|| StringUtil.trimWhiteToNull(endpointToContent.getValue()) == null) {
			throw new UIPServiceException("the http param '" + PARAM_ENDPOINT_TO + "' can't be null.");
		}
		
		ParamContent endpointFromContent = p.getParam(PARAM_ENDPOINT_FROM);
		if (endpointFromContent == null
				|| StringUtil.trimWhiteToNull(endpointFromContent.getValue()) == null) {
			throw new UIPServiceException("the http param '" + PARAM_ENDPOINT_FROM + "' can't be null.");
		}
		
		ParamContent taskType = p.getParam(PARAM_TASK_TYPE);
		if (taskType == null
				|| StringUtil.trimWhiteToNull(taskType.getValue()) == null) {
			throw new UIPServiceException("the http param '" + PARAM_TASK_TYPE + "' can't be null.");
		}
		
		String endpointTo = StringUtil.trimWhiteToNull(endpointToContent.getValue()); // to
		String endpointFrom = StringUtil.trimWhiteToNull(endpointFromContent.getValue()); // from
		String type = StringUtil.trimWhiteToNull(taskType.getValue()); // task type
		
		Endpoint et = EndpointGenerator.generate(endpointTo);
		Endpoint ef = EndpointGenerator.generate(endpointFrom);
		if (et == null || ef == null) {
			throw new UIPServiceException("the endpoint TO or FROM can't be null.");
		}
				
		et.removeAttribute(EndpointGenerator.ENDPOINT_EXPRESSION_LOCATION); // 删除location
		ef.removeAttribute(EndpointGenerator.ENDPOINT_EXPRESSION_LOCATION); // 删除location
		
		if (Type.NORMAL == Type.parse(type)) {
			return et;
		} else {
			return ef;
		}
	}
	
	/**
	 * 获取要发送的params
	 * 
	 * @param task
	 * @return
	 * @throws Exception
	 */
	public static HttpParam getPostParams(Task task, IoSession session) throws Exception {		
		ByteBuffer buffer = ByteBuffer.allocate(1024).setAutoExpand(true);		
		buffer.putObject(task);
		buffer.flip();
		
		HttpParam p = new HttpParam();
		p.setHeaderline("POST / HTTP/1.1");
		p.setContents(buffer);
		p.addParams("User-Agent", "UIP");		
		p.addParams(PARAM_ENDPOINT_FROM, task.route().from().getExpression());
		p.addParams(PARAM_ENDPOINT_TO, task.route().to().getExpression());
		p.addParams(PARAM_TASK_TYPE, task.getType().getName());
		
		if (session != null) {
			p.setSession(session);
		}
				
		return p;
	}
	
	/**
	 * 返回
	 * 
	 * @param result
	 * @param parent
	 * @return
	 * @throws Exception
	 */
	public static HttpParam getResponseParams(Task result, HttpParam parent) throws Exception {
		ByteBuffer buffer = ByteBuffer.allocate(1024).setAutoExpand(true);		
		buffer.putObject(result);
		buffer.flip();
		
		HttpParam p = new HttpParam();
		p.setContents(buffer);
		p.addParams(PARAM_ENDPOINT_FROM, result.route().from().getExpression());
		p.addParams(PARAM_ENDPOINT_TO, result.route().to().getExpression());
		p.addParams(PARAM_TASK_TYPE, result.getType().getName());
		p.setSession(parent.getSession());
		
		return p;
	}
}
